package com.example.lanouhn.firstapp.activity.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.BrokerDealerAdapter;
import com.example.lanouhn.firstapp.activity.constants.Constants;
import com.example.lanouhn.firstapp.activity.model.BrokerDealer;
import com.example.lanouhn.firstapp.activity.utils.BrokerdealerJson;
import com.example.lanouhn.firstapp.activity.utils.DialogUtil;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;
import com.example.lanouhn.firstapp.activity.view.GraduallyTextView;

import java.util.List;

public class NotifyActivity extends AppCompatActivity {

    private TextView graduallyTextView;
    private ImageView back;
    private RecyclerView newRecyclerView;
    private String key;
    //适配器
    private BrokerDealerAdapter adapter;
    //数据集合
    private List<BrokerDealer> list;

    //动态注册广播接收器的变量类型
    private MyBroadcastReceiver myBroadcastReceiver;
    /**
     * 创建内部类来实现接收广播的
     */
    class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            getDataFromWeb();
        }
    }

    //线程
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            graduallyTextView.setVisibility(View.GONE);
            super.handleMessage(msg);
            switch (msg.what) {

                case 0:

                    DialogUtil.showDialog4(NotifyActivity.this,"网络错误！",false);

                    break;
                case 1:
                    //创建adapter
                    adapter = new BrokerDealerAdapter(list, NotifyActivity.this);

                    newRecyclerView.setAdapter(adapter);
                    //执行完毕，更新UI,
                    adapter.notifyDataSetChanged();
                    //点击跳转到商品详情
                    adapter.setBrokerDealerRecyclerViewListener
                            (new BrokerDealerAdapter.
                                    BrokerDealerRecyclerViewListener() {
                                @Override
                                public void onClick(View view, int position) {
                                    Intent intent = new Intent
                                            (NotifyActivity.this, GoodsDetailsActivity.class);
                                    //携带商品id进行跳转
                                    intent.putExtra("goods_id", list.get(position).getGoods_id());
                                    startActivity(intent);

                                }
                            });

                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify);


        key = (String) SPUtils.get(NotifyActivity.this,"log_key","");
        newRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //设置图片距离
        NotifyActivity.SpacesItemDecoration decoration = new SpacesItemDecoration(9);
        //推荐商品图片的距离
        newRecyclerView.addItemDecoration(decoration);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        newRecyclerView.setLayoutManager(gridLayoutManager);

        //动态注册广播接收器
        myBroadcastReceiver = new MyBroadcastReceiver();
        //过滤器
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("notify");
        //注册
        registerReceiver(myBroadcastReceiver, mFilter);

        //找控件
        ininView();
        //点击事件
        setOnClickListener();
        //请求网络数据
        getDataFromWeb();
    }


    //请求网络数据 //开启线程
    private void getDataFromWeb() {
        graduallyTextView.setVisibility(View.VISIBLE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                //调用HttpUtils类，来请求网络获取Json数据
                String result = HttpUtils.doGet(Constants.BROKERDEALER_LIST_URL);
                //是否获取到Json数据
                if (!result.equals("") && !result.equals("null")) {

                    //将获取到的json数据解析，通过封装的工具类
                    list = BrokerdealerJson.getBrokerdealer(result);

                    //判断解析出的集合的长度，有数据就显示，无数据就不显示
                    if (list.size() > 0) {

                        //获取数据成功
                        handler.sendEmptyMessage(1);
                    } else {
                        //失败
                        handler.sendEmptyMessage(0);
                    }
                } else {
                    //获取Json数据失败
                    handler.sendEmptyMessage(0);
                }


            }
        }).start();


    }
    private void setOnClickListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                overridePendingTransition(R.anim.translate_back_in,R.anim.translate_back_out);
            }
        });
    }

    private void ininView(){
        back = (ImageView) findViewById(R.id.back);
        graduallyTextView = (TextView) findViewById(R.id.graduallyTextView);

    }
    //设置图片距离
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left = space;
            outRect.right = space;
            outRect.bottom = space;
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.top = space;
            }
        }
    }

    //注销广播接收器
    @Override
    public void onDestroy() {
        super.onDestroy();

        unregisterReceiver(myBroadcastReceiver);
    }


}
